from django.shortcuts import render, HttpResponse
from django.views.generic import View
from django.http.response import JsonResponse


# Create your views here.
def index(request):
    return HttpResponse('这是app02的index页面')


def hostlist(request):
    # 真实数据应该从数据库取出
    # 模拟取出的数据
    hostlist = [
            {
                "id": 3,
                "hostname": "server03",
                "ip": "192.168.17.102",
                "type": "web",
                "status": "up"
            },
            {
                "id": 4,
                "hostname": "server04",
                "ip": "192.168.17.103",
                "type": "lb",
                "status": "up"
            },
            {
                "id": 5,
                "hostname": "server05",
                "ip": "192.168.17.104",
                "type": "haproxy",
                "status": "down"
            },
            {
                "id": 6,
                "hostname": "server06",
                "ip": "192.168.17.106",
                "type": "mysql slave",
                "status": "up"
            },
            {
                "id": 7,
                "hostname": "server07",
                "ip": "192.168.17.106",
                "type": "mycat",
                "status": "up"
            }
    ]
    return render(request, 'hostlist.html',locals())


# 函数视图写法
def login(request):
    # get方法加载页面
    if request.method == "GET":
        return render(request, 'login.html')
    elif request.method == "POST":
        # print(request.POST)
        # 真实情况下取数据库  模拟
        user_info = request.POST
        # 从web前端传输的数据都为字符串
        if user_info.get('username') == 'admin' and user_info.get('password') == '123456':
            # return HttpResponse('登录成功')
            return HttpResponse("<script>window.location.href='http://127.0.0.1:8000/app02/hostlist'</script>")
        else:
            return HttpResponse("<script>window.location.href='http://127.0.0.1:8000/app02/login'</script>")


# 类视图写法
class LoginView(View):
    def get(self, request):
        return render(request, 'login.html')

    # 表单返回信息
    # def post(self, request):
    #     user_info = request.POST
    #     # 从web前端传输的数据都为字符串
    #     if user_info.get('username') == 'admin' and user_info.get('password') == '123456':
    #         # return HttpResponse('登录成功')
    #         return HttpResponse("<script>document.write('登录成功,3s后跳转');setTimeout( \"window.location.href='http://127.0.0.1:8000/app02/hostlist'\",3000)</script>")
    #     else:
    #         return HttpResponse("<script>document.write('登录失败,3s跳回到登录页面,');setTimeout(\"window.location.href='http://127.0.0.1:8000/app02/login'\",3000)</script>")
    # ajax返回信息
    def post(self, request):
        user_info = request.POST
        print(request.POST)
        # 从web前端传输的数据都为字符串
        if user_info.get('username') == 'admin' and user_info.get('password') == '123456':
            msg = {'status': 0, 'msg': 'login success'}
        else:
            msg = {'status': 1, 'msg': 'login fail'}
        return JsonResponse(msg)

    def delete(self, request):
        return HttpResponse('登录的删除方法')


class MyTemplateView(View):
    def get(self, request):
        name = 'devops'
        # 接收GET方式传入的参数,再传到前端页面
        # 请求传输过来的参数 都被转化字符串
        age = int(request.GET.get('age'))
        # print(type(age))
        job = 'linux'
        # 复合类型
        hobby = ['吃饭', '睡觉', '打棒球','打豆豆']
        info = {'msg': 'I am devops', 'introduction': '我是一个运维开发'}
        salary = 8000
        import datetime
        time = datetime.datetime.now()
        print(locals())
        return render(request, 'tpl.html', locals())

class UserListView(View):
    def get(self,request):
        return render(request,'userlist.html')
